package org.sys.transporttrainsys.controller;

import org.sys.transporttrainsys.entity.TrainingType;
import org.sys.transporttrainsys.service.TrainingTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin(origins = "http://localhost:5173")
@RequestMapping("/api/training-types")
public class TrainingTypeController {

    @Autowired
    private TrainingTypeService trainingTypeService;

    @GetMapping
    public List<TrainingType> getAllTrainingTypes() {
        return trainingTypeService.getAllTrainingTypes();
    }

    @GetMapping("/{id}")
    public ResponseEntity<TrainingType> getTrainingTypeById(@PathVariable Long id) {
        TrainingType trainingType = trainingTypeService.getTrainingTypeById(id);
        return ResponseEntity.ok(trainingType);
    }

    @PostMapping
    public TrainingType createTrainingType(@RequestBody TrainingType trainingType) {
        return trainingTypeService.createTrainingType(trainingType);
    }

    @PutMapping("/{id}")
    public ResponseEntity<TrainingType> updateTrainingType(@PathVariable Long id, @RequestBody TrainingType trainingType) {
        TrainingType updatedType = trainingTypeService.updateTrainingType(id, trainingType);
        return ResponseEntity.ok(updatedType);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteTrainingType(@PathVariable Long id) {
        trainingTypeService.deleteTrainingType(id);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/search")
    public List<TrainingType> searchByTypeName(@RequestParam String name) {
        return trainingTypeService.searchByTypeName(name);
    }
}